package com.oa.controller;

import com.alibaba.fastjson.JSONObject;
import com.oa.pojo.*;
import com.oa.service.DeptService;
import com.oa.utils.EntityException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Str
 * @Date 2021/12/13 10:23
 * @Description :
 */
@Controller
@RequestMapping("/dept")
public class DeptController extends BaseController {
    @Autowired
    private DeptService deptService;

    @RequestMapping("/showDeptList")
    public String showDeptList() {
        return "/dept/deptList";
    }

    // 返回Dept JSON数据
    @RequestMapping(value = "/jsonDeptList", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String jsonMyInitiate(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        String respTxt = "";
        try {
            // 查询数据
            PageBean<Dept> deptPageBean = deptService.getAll(page, limit);
            respTxt = packJSON(deptPageBean);
        } catch (Exception e) {
            // 封装错误信息
            respTxt = setErrorMsg(e);
        }
        return respTxt;
    }

    // 跳转到新增部门页
    @RequestMapping("/showDeptForm")
    public String showDeptForm() {
        return "/dept/deptForm";
    }

    //3.1、jsonDeptOptions
    @RequestMapping(
            value = "/jsonDeptOptions",
            produces = "text/html;charset=UTF-8"
    )
    @ResponseBody
    public String jsonDeptOptions() {
        List<Option> list = deptService.getDeptOptions();
        JSONObject jsObj = new JSONObject();
        jsObj.put("deptJson", list);
        return jsObj.toJSONString();
    }

    // 根据ID获取用户
    @RequestMapping(
            value = "/getDept",
            produces = "text/html;charset=UTF-8"
    )
    @ResponseBody
    public String getDept(@RequestParam("deptId") String deptId){
        Map<String, Object> map = new HashMap<>();
        Dept dept = null;
        JSONObject jsObj = new JSONObject();
        String respTxt = "";
        try {
            //{2}根据 id 获取用户
            dept = deptService.getOneById(deptId);
            //{3}存入相关信息
            jsObj.put("result", "success");
            jsObj.put("dept", dept);
            respTxt = jsObj.toString();
        } catch (Exception e) {
            e.printStackTrace();
            respTxt = setErrorMsg(e); //打包错误信息
        }
        return respTxt;
    }

    @RequestMapping(
            value = "/saveDept",
            produces = "text/html;charset=UTF-8"
    )
    @ResponseBody
    public String saveDept(@RequestParam Map<String, String> data) {
        for (String s : data.keySet()) {
            System.out.println("key:" + s + ",value:" + data.get(s));
        }

        JSONObject jsObj = new JSONObject();
        String respTxt = "";
        try {
            deptService.saveDept(data);
            jsObj.put("result", "success");
            respTxt = jsObj.toString();
        } catch (EntityException e) {
            e.printStackTrace();
            respTxt = setErrorMsg(e);
        }
        return respTxt;
    }


    @RequestMapping(
            value = "/delDept",
            produces = "text/html;charset=UTF-8"
    )
    @ResponseBody
    public String delDept(@RequestParam("deptId") String deptId) {


        JSONObject jsObj = new JSONObject();
        String respTxt = "";
        try {
            deptService.delDept(deptId);
            jsObj.put("result", "success");
            respTxt = jsObj.toString();
        } catch (EntityException e) {
            e.printStackTrace();
            respTxt = setErrorMsg(e);
        }
        return respTxt;
    }
}
